<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

namespace Common\Library\COM;

/**
 * Description of OrderDo
 *
 * @author gangguo
 */
class CollectDo {
    //put your code here

    static public function num(){
        return array('1','2','3','4','5','6','7','8','9','0','一','二','三','四','五','六','七','八','九','零','两','壹','贰','叁','肆','伍','陆','柒','捌','玖','俩','十','百','千','万');
    }

    //章节名称中获取文章章节数
    static public function getArticleNum($str = ''){
        if(empty($str)){
            return false;
        }
        $str = trim($str);
        $article_num = preg_split("//u", $str);
        $nums = null;
        $num = self::num();
        foreach ($article_num as $k => $v) {
            # code...
            if(empty($v) && $v != '0'){
                continue;
            }
            if(in_array($v, $num)){
                $nums .= $v;
            }else{
                if($v != '第'){
                    break;
                }
            }
        }
        return CollectDo::returnInt($nums);
    }

    //获取篇数目
    static public function getPieceNum($str = ''){
        if(empty($str)){
            return 0;
        }
        $str = trim($str);
        $article_num = preg_split("//u", $str);
        $nums = null;
        $num = self::num();
        foreach ($article_num as $k => $v) {
            # code...
            if(empty($v) && $v != '0'){
                continue;
            }
            if(in_array($v,$num)){
                $nums .= $v;
            }else{
                if($v == '篇' || $v == '卷'){
                    break;
                }
            }
        }
        return CollectDo::returnInt($nums);
    }


    //字符串的章节数转为整型的章节数
    static public function returnInt($str){
        if(empty($str)){
            return 0;
        }
        if($str == '十'){
            return '10';
        }
        $keyNum=array('零'=>0,'一'=>1,'二'=>2,'三'=>3,'四'=>4,'五'=>5,'六'=>6,'七'=>7,'八'=>8,'九'=>9,'壹'=>1,'贰'=>2,'叁'=>3,'肆'=>4,'伍'=>5,'陆'=>6,'柒'=>7,'捌'=>8,'玖'=>9,'俩'=>2,'两'=>2);
        $length = mb_strlen($str);
        $str = str_replace(array_keys($keyNum), array_values($keyNum), $str);
        if(is_numeric($str)){
            return $str;
        }
        $arr = preg_split("//u", $str);
        $arr = array_filter($arr);
        $forNum = count($arr);
        $forNum = ($forNum-1)/2;
        $chr = array('十'=>10,'百'=>100,'千'=>1000,'万'=>10000);
        for($i = 0;$i<$forNum;$i++){
            $a_num = current($arr);
            next($arr);
            $a_chr = current($arr);
            next($arr);
            $articleNum += intval($a_num) * ($chr[$a_chr]);
        }
        $articleNum += current($arr);
        //p($articleNum);exit;
        return $articleNum;
    }

    //下载页面
    static public function downPage($url = '',$pathFile = ''){
        if(empty($url) || empty($pathFile)){
            return false;
        }
        $dir = pathinfo($pathFile);
        if(!is_dir($dir['dirname'])){
            mkdir($dir['dirname'],0777,true);
        }
        shell_exec("wget --no-cache -O '{$pathFile}' {$url}");
        //$data = file_get_contents($url);
        //file_put_contents($pathFile, $data);
        if(file_exists($pathFile)){
            return true;
        }else{
            return false;
        }
        //return \Org\Net\Http::fsockopenDownload($url,array('int'=>20));
    }

    //截取字符串
    static public function getSubstr($str = '',$startHtml = '',$endHtml = '',$charset = 'utf-8',$offset = 0){
        if(empty($str)){return false;}
        if(empty($startHtml) && empty($endHtml)){return false;}
        if(empty($startHtml)){
            $start = 0;
            $length = 0;
        }else{
            if($charset){
                $start = mb_strpos($str,$startHtml,$offset,$charset);
                $length = mb_strlen($startHtml,$charset);
            }else{
                $start = mb_strpos($str,$startHtml,$offset);
                $length = mb_strlen($startHtml);
            }
        }
        if(empty($endHtml)){
            return mb_substr($str, $start+$length);
        }

        if($charset){
            $end = mb_strpos($str,$endHtml,$offset,$charset);
            //p($startHtml);p($start);p($end);
            $str = mb_substr($str, $start+$length,$end-$start-$length,$charset);
        }else{
            $end = mb_strpos($str,$endHtml,$offset);
            //p($startHtml);p($start);p($end);
            $str = mb_substr($str, $start+$length,$end-$start-$length);
        }
        return trim($str);
    }


    //获取html内容并转换字符集
    static public function getHtml($htmlPath = '',$toCharset = 'utf-8',$charset = 'gbk'){
        if(file_exists($htmlPath)){
            $str = file_get_contents($htmlPath);
            //$charset = mb_detect_encoding($str,array('ASCII','GB2312','GBK','UTF-8'));//检查字符串编码
            if($charset == 'utf-8'){
                return $str;
            }
            return mb_convert_encoding($str,$toCharset,$charset);
        }
    }

    //检查特殊字符是否存在 false:有特殊字符
    static public function checkSpecialStr($str = null){
        if(empty($str)){
            return false;
        }
        $onu = strpos($str,'script');
        $fou = strpos($str,'<a');
        $fiv = strpos($str,'/a>');
        if(empty($str)){
            return false;
        }
        return true;
    }

    //获取封面图片存储地址
    static public function doImagePath(){
        $path = '/Public/location/img/'.date('Y',time()).'/'. mt_rand(0, 9).mt_rand(0,9);
        if(!file_exists($path)){
            mkdir('.'.$path, 0777,TRUE);
        }
        return $path.'/'.mt_rand(0,9).mt_rand(0,9).mt_rand(0, 9).mt_rand(0, 9).mt_rand(0, 9).'.png';
    }

    //获取内容存储地址
    static public function doContentPath(){
        $path = '/Public/location/content/'.date('Y',time()).'/'.mt_rand(0,9).mt_rand(0,9);
        if(!file_exists($path)){
            mkdir('.'.$path, 0777,TRUE);
        }
        $path = $path.'/'.date('YmdHis',time()).'-'.mt_rand(0,9).mt_rand(0, 9).mt_rand(0, 9).mt_rand(0, 9).'.txt';
        if(file_exists($path)){
            self::doContentPath();
        }else{
            return $path;
        }
    }

    //统一小说分类名称
    static public function changeCateName($cate_name){
        if(empty($cate_name)){
            return false;//空类处理
        }
        $cates = array(
                '玄幻'=>array('玄幻','玄幻小说','玄幻奇幻','玄幻魔法','恐怖悬疑','魔幻言情','东方玄幻'),
                '都市'=>array('都市','都市小说','都市言情','耽美言情','古代言情','现代言情','言情小说','官场小说','言情纯爱','美文','综合·爱情','侦探小说'),
                '修真'=>array('修真','修真小说','武侠仙侠','武侠小说','综合其他'),
                '穿越'=>array('穿越','穿越小说','历史军事','古代历史','历史小说','架空历史','传记纪实','军事小说'),
                '网游'=>array('网游','网游小说','网游动漫','网游竞技','网络小说'),
                '科幻'=>array('科幻','科幻小说','科幻末世','科幻灵异','恐怖小说','盗墓小说'),
                '女生'=>array('女生','女生小说','女生频道'),
                '文学'=>array('文学','世界名著','文学经典','经典文学','名著','四大名著','现代文学','青春文学','投资金融','外国文学','古典文学','诗词'),
            );
        foreach ($cates as $key => $value) {
            if(in_array($cate_name, $value)){
                return $key;
            }
        }
        return false;
    }

    static public function books($bookName = ''){
        if(empty($bookName)){
            //return false;
        }
        $books = array('');
    }

    //百度地址收录推送
    static public function baiduSeo($urls = array('http://www.baidu.com/index.html')){
        $api = 'http://data.zz.baidu.com/urls?site=www.896zw.com&token=AJcyuOvJuAHB8Dwy';
        $ch = curl_init();
        $options =  array(
            CURLOPT_URL => $api,
            CURLOPT_POST => true,
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_POSTFIELDS => implode("\n", $urls),
            CURLOPT_HTTPHEADER => array('Content-Type: text/plain'),
        );
        curl_setopt_array($ch, $options);
        $result = curl_exec($ch);
        echo $result;
    }

}
